//
//  FileController.swift
//  CreateMyCode
//
//  Created by zhibao on 2024/2/1.
//

import UIKit

class FileController: SecSuperController,UITableViewDataSource, DataChangeDelegate, UITableViewDelegate{
    
   
    fileprivate var showTableView:MYTableView!
    fileprivate var dataArray = [[String:Any]]()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.getData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.gk_navTitle = LocalizedString("文件")
        self.initTableView()
    }
    
    fileprivate func getData(){
        if !UserInfoManager.shared().isLogin {
            QMUITips.showInfo(LocalizedString("网络异常，请稍后再试"))
            UserInfoManager.shared().userInit()
            return
        }
        
        HttpHelper.sharedInstance().sendGet("api/qrcode/list", parameters: [:]) { operation, responseObject in
            let data = responseObject.data as? [String:Any] ?? [String:Any]()
            self.dataArray = data["data"] as! [[String : Any]];
            self.showTableView.reloadData()
        } failure: { operation, error in
            
        }

    }
    
    fileprivate func initTableView(){
        showTableView = MYTableView(frame: CGRect(x: 0, y: navigationFullHeight(), width: SCREEN_WIDTH, height: SCREEN_HEIGHT - navigationFullHeight() - tabBarFullHeight()), style: .plain)
        
        showTableView.delegate = self
        showTableView.dataSource = self
        showTableView.dataChangeDelegate = self
        showTableView.backgroundColor = UIColor.clear
        showTableView.noDataType = .TableViewNoDataType_Data
        showTableView.separatorStyle = .none
        //showTableView.removeMJRefresh()
        self.view.addSubview(showTableView)
    }
}

extension FileController{
    func loadMoreData(withTabelViewTag tableViewTag: Int) {
        
    }
    
    func refreshData(withTabelViewTag tableViewTag: Int) {
        self.getData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var identifier = "savecell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? FileCell
        if cell == nil {
            cell = FileCell(style: .default, reuseIdentifier: identifier)
            cell?.selectionStyle = .none
        }
        
        cell?.update(dic: dataArray[indexPath.row])
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if !UserInfoManager.shared().isVip(){
            Helper.alertShow(withTitle: LocalizedString("提示"), withContent: LocalizedString("开通该会员才能查看哦！"), withSureBtnTitle: LocalizedString("开通"), withCancelBtnTitle: LocalizedString("取消")) { index in
                if index == 1 {
                    let controller = PayMarkController()
                    self.navigationController?.pushViewController(controller, animated: true)
                }
            }
          return
        }
        let dic = dataArray[indexPath.row]
        let controller = FileDetailController()
        controller.dic = dic
        self.navigationController?.pushViewController(controller, animated: true)
    }
}

